<template>
	<view class="container page">
		<Header :isBack="true">
			<block slot="content">{{title}}</block>
		</Header>
		<view class="">
			<view class="list-cell b-b m-t" @click="goComment" hover-class="cell-hover" :hover-stay-time="50">
				<image class="images" src="/static/images/gongsixinxi.png"></image>
				<text class="cell-tit">去评价</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b m-t" @click="goImage(1)" hover-class="cell-hover" :hover-stay-time="50">
				<image class="images" src="/static/images/gongsixinxi.png"></image>
				<text class="cell-tit">营业执照</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b m-t" @click="goImage(2)" hover-class="cell-hover" :hover-stay-time="50">
				<image class="images" src="/static/images/gongsixinxi.png"></image>
				<text class="cell-tit">人力资源许可证</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b m-t" @click="company" hover-class="cell-hover" :hover-stay-time="50">
				<image class="images" src="/static/images/gongsixinxi.png"></image>
				<text class="cell-tit">公司信息</text>
				<text class="cell-more yticon icon-you"></text>
			</view>

			<view class="list-cell b-b" @tap="getWechat" v-show="showBingWX">
				<image class="images" src="/static/images/weixin1.png" style="width:38rpx;height:31rpx;"></image>
				<text class="cell-tit">一键绑定微信</text>
				<text class="cell-tip">{{bindWxMsg}}</text>
				<text class="cell-more yticon icon-you"></text>
			</view>

			<view class="list-cell b-b" @tap="getApple" v-show="showBingApple">
				<image class="images" src="/static/images/appleb.png" style="width:38rpx;height:31rpx;"></image>
				<text class="cell-tit">一键绑定苹果</text>
				<text class="cell-tip">{{bindAppleMsg}}</text>
				<text class="cell-more yticon icon-you"></text>
			</view>

			<view class="list-cell b-b" @tap="password">
				<image class="images" src="/static/images/zhuanghuyuanquan.png"></image>
				<text class="cell-tit">账户与安全</text>
				<text class="cell-more yticon icon-you"></text>
			</view>

			<view class="list-cell b-b" @tap="privacy">
				<image class="images" src="/static/images/anquan.png"></image>
				<text class="cell-tit">隐私政策</text>
				<text class="cell-more yticon icon-you"></text>
			</view>

			<!-- #ifdef APP-PLUS -->
			<view class="list-cell" @tap="getNewVersion">
				<image class="images" src="/static/images/banbenhao.png"></image>
				<text class="cell-tit">检查更新</text>
				<text class="cell-tip">当前版本 {{version}}</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<!-- #endif -->

			<view class="list-cell log-out-btn" @click="toLogout">
				<text class="cell-tit" style="margin-left:0;">退出登录</text>
			</view>
		</view>

		<view class="copyright">
			Copyright © 2020-2022 河南廉德实业有限公司www.hnliandesy.com 版权所有
		</view>

		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" title="退出确认" content="" @confirm="dialogConfirm">
				<view class="" style="font-size: 30rpx;">
					确定要退出登录么
				</view>
			</uni-popup-dialog>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				title: '设置',
				version: '',
				wechatid: 0,
				bindWxMsg: '未绑定',
				bindAppleMsg: '未绑定',
				showBingWX: false,
				showBingApple: false,

				appid: "wx5f6ba2c4d12e2e1f",
				unionId: '',
				msgType: 'info',
			};
		},
		onLoad(option) {
			// let result = this.$api.isLogin();
			// if (result === false) {
			// 	return false;
			// }

			const systemInfo = uni.getSystemInfoSync()
			this.version = systemInfo.appWgtVersion;

			this.getInfo();

			// #ifdef APP-PLUS

			if (uni.getSystemInfoSync().platform == 'ios') {
				this.showBingApple = true;

				if (plus.runtime.isApplicationExist({
						pname: 'com.tencent.mm',
						action: 'weixin://'
					})) {
					this.showBingWX = true;
				} else {
					this.showBingWX = false
				}
			}
			// #endif

			// #ifdef MP-WEIXIN
			this.showBingWX = true;
			// #endif

			// #ifdef H5
			if (this.$wechat && this.$wechat.isWechat()) {
				this.showBingWX = true;

				//微信公众号登录
				if (!this.unionId) {
					if (window.location.href.indexOf('?code=') !== -1 || window.location.href.indexOf('&code=') !== -1) {
						let code;
						if (window.location.href.indexOf('?code=') !== -1) {
							code = window.location.href.split('?code=')[1].split('&')[0];
						} else {
							code = window.location.href.split('&code=')[1].split('&')[0];
						}
						//请求后端网络请求
						this.getWxOpenId(code);
					}
				}
			}
			// #endif

		},
		methods: {
			...mapMutations(['logout']),

			async getWxOpenId(code) {
				let res = await this.$myRequest({
					url: '/getWxOpenId',
					data: {
						code: code
					}
				})

				if (res.data.status == 1) {
					let info = res.data.data
					let dataObj = {
						openId: info.openid,
						nickName: info.nickname,
						avatarUrl: info.headimgurl,
						unionId: info.unionid,
						type: 2
					}
					this.bindWx(dataObj)
				}
			},

			async getInfo() {
				let token = uni.getStorageSync('token');
				let cate = uni.getStorageSync('cate');

				if (!token) {
					this.$api.msg('token缺失！');
				}

				let res = await this.$myRequest({
					url: '/getComInfo',
					data: {
						cate: cate,
						token: token
					},
					methods: 'post'
				})

				let info = res.data.data;
				this.wechatid = info.wechatid;
				this.bindMsg = info.wechatid > 0 ? '已绑定' : '未绑定';
			},

			goComment() {
				uni.navigateTo({
					url: "/pages4/pages/company-set/comment"
				})
			},

			goImage(type) {
				uni.navigateTo({
					url: "/pages4/pages/company-set/image?type=" + type
				})
			},

			//跳转到个人资料
			company() {
				uni.navigateTo({
					url: "/pages4/pages/company/company"
				})
			},

			//检查更新
			async getNewVersion() {
				const systemInfo = uni.getSystemInfoSync()
				let current_verson = systemInfo.appWgtVersion;
				let res = await this.$myRequest({
					url: '/getSoftVersion'
				})

				if (res.data.status == 1) {
					if (current_verson != res.data.data) {
						uni.showModal({
							content: '有新的版本，是否更新？',
							success: (e) => {
								if (e.confirm) {
									let url = this.$downLoadUrl;
									// #ifdef H5
									Window.location.href = url
									// #endif

									// #ifdef APP-PLUS
									uni.downloadFile({
										url, //文件链接
										success: (data) => {
											if (data.statusCode === 200) {
												this.$api.msg('正在下载中...', 'success')
												//文件保存到本地
												uni.saveFile({
													tempFilePath: data
														.tempFilePath, //临时路径
													success: (res) => {
														this.$api.msg('软件已下载',
															'success')
														setTimeout(() => {
															//打开文档查看
															uni.openDocument({
																filePath: res
																	.savedFilePath,
																success: (
																	res
																) => {
																	// console.log('打开文档成功');
																}
															});
														}, 1500)
													}
												});
											}
										},
										fail: (err) => {
											this.$api.msg('失败请重新下载')
										},
									})
									// #endif
								}
							},

						});
					} else {
						this.$api.msg('当前已是最新版本，无须更新！')
					}
				}
			},

			//退出登录
			toLogout() {
				this.$refs.alertDialog.open()
			},

			async dialogConfirm() {
				this.logout();
				uni.setStorageSync('token', '')
				uni.setStorageSync('cate', 0)
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}, 600)
			},

			//获取微信信息
			async getWechat() {
				if (this.wechatid > 0) {
					this.$api.msg('已绑定微信！')
					return false;
				}
				// #ifdef APP-PLUS
				if (plus.runtime.isApplicationExist({
						pname: 'com.tencent.mm',
						action: 'weixin://'
					})) {

					uni.login({
						provider: 'weixin',
						success: (loginRes) => {
							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: (infoRes) => {
									infoRes.userInfo.type = 3;
									this.bindWx(infoRes.userInfo)
								}
							});
						}
					});

				} else {
					this.$api.msg('微信应用未安装')
				}
				// #endif

				// #ifdef H5
				if (this.$wechat && this.$wechat.isWechat()) {
					//微信浏览器
					this.getAccessToken();
				} else {
					//非微信浏览器
				}
				// #endif

				// #ifdef MP-WEIXIN
				var that = this;
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								let dataObj = {
									code: loginRes.code,
									encryptedData: infoRes.encryptedData,
									iv: infoRes.iv,
									nickName: infoRes.userInfo.nickName,
									avatarUrl: infoRes.userInfo.avatarUrl,
									type: 1
								}
								that.bindWx(dataObj)
							}
						});
					}
				});
				// #endif
			},

			async getAccessToken() {
				//重点 这里getWxAppid这个是微信公众号的appid
				if (!this.unionId) {
					window.location.href =
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
						this.appid +
						'&redirect_uri=' + encodeURIComponent(
							'https://test.hnliandesy.com/h5/#/pages4/pages/company-set/company-set') +
						'&response_type=code&scope=snsapi_userinfo#wechat_redirect';
				} else {
					return false;
				}
			},

			//绑定微信
			async bindWx(dataObj) {
				dataObj.token = uni.getStorageSync('token')
				dataObj.cate = 2
				let res = await this.$myRequest({
					url: '/bindWx',
					data: dataObj,
					methods: 'post'
				})

				if (res.data.status == 1) {
					this.$api.msg(res.data.msg);
					this.wechatid = res.data.data;
					this.bindWxMsg = '已绑定'
				}
			},

			//获取苹果信息
			async getApple() {

				// #ifdef APP-PLUS
				let that = this;
				if (uni.getSystemInfoSync().platform == 'ios') {
					if (that.appleid > 0) {
						that.unBindApple()
					} else {
						uni.login({
							provider: 'apple',
							success: function(loginRes) {
								// console.log(loginRes)
								let openId = loginRes.authResult.openid;
								let dataObj = {
									openId: openId
								}
								that.bindApple(dataObj)
							},
							fail: function(err) {
								// 登录授权失败  
								// err.code错误码参考`授权失败错误码(code)说明`
								console.log(err)
							}
						});
					}

				} else {
					that.$api.msg('非苹果系统不可绑定！')
				}
				// #endif
			},

			//绑定苹果
			async bindApple(dataObj) {
				dataObj.token = uni.getStorageSync('token')
				dataObj.cate = 2
				let res = await this.$myRequest({
					url: '/bindApple',
					data: dataObj,
					methods: 'post'
				})

				if (res.data.status == 1) {
					this.$api.msg(res.data.msg);
					this.appleid = res.data.data;
					this.bindAppleMsg = '已绑定'
				}
			},

			//解绑苹果
			async unBindApple() {
				let dataObj = {
					token: uni.getStorageSync('token'),
					cate: 2
				}

				let res = await this.$myRequest({
					url: '/unBindApple',
					data: dataObj,
					methods: 'post'
				})

				if (res.data.status == 1) {
					this.$api.msg(res.data.msg);
					this.appleid = 0;
					this.bindAppleMsg = '未绑定'
				}
			},

			//关于密码
			async password() {
				uni.navigateTo({
					url: '/pages/passwords/passwords'
				})
			},

			async privacy() {
				uni.navigateTo({
					url: '/pages6/pages/docs/privacy-policy'
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #f7f7f7;
		font-size: $font-base;
	}
</style>
<style lang='scss'>
	.container {
		// #ifdef APP-PLUS
		padding-top: 160rpx;
		// #endif

		// #ifdef MP-WEIXIN
		padding-top: 160rpx;
		// #endif

		// #ifdef H5
		padding-top: 100rpx;
		// #endif
	}

	.list-cell {
		font-size: $font-base;
		display: flex;
		align-items: baseline;
		padding: 20upx $page-row-spacing;
		line-height: 60upx;
		position: relative;
		background: #fff;
		justify-content: center;

		&.log-out-btn {
			margin-top: 40upx;

			.cell-tit {
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-more {
			align-self: baseline;
			/* font-size: $font-lg; */
			color: $font-color-light;
			margin-left: 10upx;
		}

		.cell-tit {
			flex: 1;
			/* font-size: $font-base + 2upx; */
			color: $font-color-dark;
			margin-right: 10upx;
			margin-left: 50rpx;
		}

		.cell-tip {
			/* font-size: $font-base; */
			color: $font-color-light;
		}

		switch {
			transform: translateX(16upx) scale(.84);
		}
	}

	.copyright {
		width: 70%;
		position: absolute;
		text-align: center;
		margin-left: 15%;
		top: 1160rpx;
		color: #909399;
		font-size: 26rpx;
	}

	.images {
		width: 28rpx;
		height: 32rpx;
		margin-right: 12px;
		position: absolute;
		left: 30rpx;
		top: 32rpx;
	}
</style>